/*
 * Copyright 2013 kubi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.PA165.musiclibraryweb.register;

import cz.muni.fi.PA165.musiclibraryweb.HomePage;
import cz.muni.fi.PA165.musiclibraryapi.services.UserService;
import cz.muni.fi.PA165.musiclibrarybackend.dto.UserDTO;
import cz.muni.fi.PA165.musiclibrarybackend.entities.User;
import cz.muni.fi.PA165.musiclibraryweb.signin.SignIn;
import java.io.Serializable;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author kubi
 */
public class Register extends WebPage implements Serializable {
    @SpringBean
    private Label errorMessage;

    private UserService service;
    private UserDTO user;



    public Register(PageParameters parameters) {
        super(parameters);
       
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        service = context.getBean(UserService.class);

        
        add(new Label("title", new StringResourceModel("title", this, null)));
        
        RegisterForm registerForm = new RegisterForm("registerForm");
        registerForm.setParameters(parameters);
        add(registerForm);
        errorMessage = new Label("error", "");
        errorMessage.setVisible(false);
        add(errorMessage);
    }    
    
    public Register(PageParameters parameters,Label label) {
        super(parameters);



        add(new Label("title", new StringResourceModel("title", this, null)));
        
        RegisterForm registerForm = new RegisterForm("registerForm");
        registerForm.setParameters(parameters);
        add(registerForm);
        
       if (label == null) {
            errorMessage = new Label("error", "");
            errorMessage.setVisible(false);
        } else {
            errorMessage = label;
            errorMessage.setVisible(true);
        }
        add(errorMessage);
        
    }
    
    public final class RegisterForm extends Form<Void>
    {
        private static final String USERNAME = "username";
        private static final String PASSWORD = "password";
        private static final String PASSWORD2 = "password2";
        
        private PageParameters parameters;
        private Label errorMessage;
        

        
   
        
        // El-cheapo model for form
        private final ValueMap properties = new ValueMap();

        /**
         * Constructor
         * 
         * @param id
         *            id of the form component
         */
        public RegisterForm(final String id)
        {
            super(id);
            
            // Attach textfield components that edit properties map model
            add(new TextField<String>(USERNAME, new PropertyModel<String>(properties, USERNAME)));
            add(new PasswordTextField(PASSWORD, new PropertyModel<String>(properties, PASSWORD)));
            add(new PasswordTextField(PASSWORD2, new PropertyModel<String>(properties, PASSWORD2)));
        }

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @Override
        public final void onSubmit()
        {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            service = context.getBean(UserService.class);
            
            user = new UserDTO();
            
            
            user.setName(getUsername());
            
            if (getPassword().equals(getPassword2())) {
                user.setPassword(getPassword());
                user.setAdmin(Boolean.FALSE);  
                        
                try {
                    service.createUser(user);
                    setResponsePage(new SignIn(parameters));
                }
                catch (Exception exception) {
                    String errmsg = getString("userError", null);
                    setResponsePage(new Register(parameters,
                    new Label("error", errmsg)));
                }
            }
            else {
                String errmsg = getString("passwordError", null);
                setResponsePage(new Register(parameters, new Label("error", errmsg)));
            }
                      
            
    
            
            
            
            
            

         
        }

        /**
         * @return
         */
        private String getPassword()
        {
            return properties.getString(PASSWORD);
        }
         /**
         * @return
         */
        private String getPassword2()
        {
            return properties.getString(PASSWORD2);
        }

        /**
         * @return
         */
        private String getUsername()
        {
            return properties.getString(USERNAME);
        }



        public PageParameters getParameters() {
            return parameters;
        }

        public void setParameters(PageParameters parameters) {
            this.parameters = parameters;
        }
        
        
    }
}
